package test;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.brother.model.LatLngPoint;
import com.brother.model.RealTimeData;
import com.brother.model.Signal;
import com.brother.util.NigeriaAddress;
import com.brother.util.PointIntersect;

public class FindAreaIMEIS {

	/**
	 * @param args
	 * @throws SQLException 
	 */
	public static void main(String[] args) throws SQLException {
		
		// lng    lat   lng     lat
		// 99.612,0.854,119.926,7.6253333333333355
		// -72.286148,6.425727;-31.329117,6.425727;-31.329117,-34.069826;-72.286148,-34.069826
		List<LatLngPoint> bounds = new ArrayList<LatLngPoint>();
		LatLngPoint p = new LatLngPoint(-72.286148f, 6.425727f);
		bounds.add(p);
		p = new LatLngPoint(-31.329117f, 6.425727f);
		bounds.add(p);
		p = new LatLngPoint(-31.329117f, -34.069826f);
		bounds.add(p);
		p = new LatLngPoint(-72.286148f, -34.069826f);
		bounds.add(p);
		
		
		Connection conn = DriverManager.getConnection("jdbc:mysql://www.smart-tracking.com:9999/gpsMap?characterEncoding=utf-8", "cong", "29749716");
		Statement stmt = conn.createStatement();
        
		String sql = "select trackerid,trackername,usertrackerid from tracker";
		ResultSet rs = stmt.executeQuery(sql);
		List<String> trackers = new ArrayList<String>();
		List<String> imeis = new ArrayList<String>();
		List<String> trackerNames = new ArrayList<String>();
		while(rs.next()){
			System.out.println(rs.getString("trackerid")+" imei=" 
					+ rs.getString("usertrackerid")+" name="+rs.getString("trackername"));
			trackers.add(rs.getString("trackerid"));
			imeis.add(rs.getString("usertrackerid"));
			trackerNames.add(rs.getString("trackername"));
		}
		for(int i=0;i<trackers.size(); i++){
			String trackerId = trackers.get(i);
			String imei = imeis.get(i);
			sql = "select * from r_"+imei+" order by createTime desc limit 0,1";
			//System.out.println("max sql="+sql);
			try{
			  rs= stmt.executeQuery(sql);
			}catch(Exception e){
				//System.err.println(e.getMessage());
				//System.out.println("error 888888888 imei="+imei+" name="+trackerNames.get(i));
				continue;
			}
			
			if(rs.next()){
//				System.out.println(rs.getString("address")+" "+rs.getString("lat")+"  "+rs.getString("lng")
//						+rs.getString("latorient")+"  "+rs.getString("lngorient")+"  "+rs.getTimestamp("createtime"));
				
				    float lng = rs.getFloat("signed_lng");
				    float lat = rs.getFloat("signed_lat");
				    if(trackerNames.get(i).startsWith("Z")){
				    	//System.out.println("brazil lng="+lng+",lat="+lat);
				    }
				    if(NigeriaAddress.getInstance().isIntesect(lat, lng)){
				    	System.out.println("imei="+imei+" name="+trackerNames.get(i));
				    }
				    
				    
//					final RealTimeData data = new RealTimeData();
//					data.setOrient("0");
//					data.setOrig_lat(rs.getString("orig_lat"));
//					data.setLatOrient(rs.getString("latorient"));
//					data.setLat(rs.getFloat("lat"));
//					
//					data.setOrig_lng(rs.getString("orig_lng"));
//					data.setLng(rs.getFloat("lng"));
//					data.setLngOrient(rs.getString("lngorient"));
//					
//					data.setTrackerid(trackerId);
//					data.setState(rs.getString("state"));
//					data.setSignal(Signal.ok.name());
//					data.setSpeed(rs.getFloat("speed"));
//					
//					data.setDistance("0");
//					data.setTraceDateTime(rs.getTimestamp("createtime"));
//					data.setLanguage(rs.getString("language"));
//					data.setAddress(rs.getString("address"));
			}
			
		}
		
		conn.close();
	}

}
